<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';

session_name(SESSION);
session_start();

$myPdo = DbUtils::createPdoInst();

$order_id = MiscUtils::getParam('id', 0);
$secret_pass = MiscUtils::getParam('secret_pass', 1);

$cond_vals = new stdClass();
$cond_vals->c = 't.id = :v1';
$cond_vals->v = array(':v1' => $order_id);
$tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;


if (count($tmp) == 0) {
  die('Order is not found');
}

$my_order = $tmp[0];

$profile = new stdClass();
$tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'user_profile', NULL, NULL, NULL, NULL, NULL, NULL)->d;
if (count($tmp) > 0) {
  $profile = $tmp[0];
}

$cond_vals = new stdClass();
$cond_vals->c = 't.order_id = :v1';
$cond_vals->v = array(':v1' => $my_order->id);
$purchases = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_purchase', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
}
div.wrap {
  border: 1px solid #666;
  padding: 5px;
  max-width: 950px;
  background: #fff;
}
div.wrap h3 {
  font-size: 16px;
  margin: 0;
}
table.lo {
  width: 100%;
  border-top: 1px solid #666;
  margin-top: 5px;
}
table.lo td {
  vertical-align: top;
}
table.form {
  background-color: #fff;
  border-top: 1px solid #aaa;
  border-left: 1px solid #aaa;
  width: 100%;
}
table.form td {
  padding: 3px 5px; 
  vertical-align: top;
}
table.form td.title {
  background-color: #ddd !important;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
  width: 100px;
}
table.form td.content {
  padding: 0 0 0 5px;
  border-bottom: 1px solid #aaa;
  border-right: 1px solid #aaa;
  vertical-align: middle !important;
  color: #999;
}
table.form td.content input[type=text], textarea {
  border: 0;
  width: 99%;
  background: #eee;
  font-weight: bold;
}
table.list {
  border-left: 1px solid #aaa;
  border-top: 1px solid #aaa;
  font-size: 13px;
  width: 100%;
}
table.list th.sort {
  text-align: left;
  height: 28px;
  background-color: #ddd;
  padding: 0 5px; 
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  font-weight: bold;
  vertical-align: middle;
  cursor: pointer;
  color: #333;
}
table.list tr.rowodd td {
  padding: 0 5px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
  overflow: hidden;
  color: #999;
}
table.list tr.roweven td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  word-wrap: break-word;
  vertical-align: middle;
  color: #999;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.list tr.none td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
table.list input[type=text] {
  width: 99%;
  border: none;
}
.imgbtn {
  background-image: url(images/save.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}

</style>
</head>
<body>
<div>
  <div class="wrap">
    <h3><?php echo $profile->shop_name; ?></h3>
    <table class="lo" cellpadding=0 cellspacing=0>
      <tr>
        <td style="width: 60%;">
          <div>&nbsp;</div>
          <b>POST ADDRESS</b>
          <table class="form" cellpadding=0 cellspacing=0>
            <tr>
              <td class="title">First Name</td>
              <td class="content"><input id="first_name" type="text" value="<?php echo $my_order->first_name; ?>"></input></td>
            </tr>
            <tr>
              <td class="title">Last Name</td>
              <td class="content"><input id="last_name" type="text" value="<?php echo $my_order->last_name; ?>"></input></td>
            </tr>
            <tr>
              <td class="title">Address1</td>
              <td class="content"><input id="address1" type="text" value="<?php echo $my_order->address1; ?>"></input></td>
            </tr>
            <tr>
              <td class="title">Address2</td>
              <td class="content"><input id="address2" type="text" value="<?php echo $my_order->address2; ?>"></input></td>
            </tr>
            <tr>
              <td class="title">City</td>
              <td class="content"><input id="city" type="text" value="<?php echo $my_order->city; ?>"></input></td>
            </tr>
            <tr>
              <td class="title">Country</td>
              <td class="content"><input id="country" type="text" value="<?php echo $my_order->country; ?>"></input></td>
            </tr>
            <tr>
              <td class="title">Postal Code</td>
              <td class="content"><input id="zip" type="text" value="<?php echo $my_order->zip; ?>"></input></td>
            </tr>
            <tr>
              <td class="title">Phone</td>
              <td class="content"><input id="phone" type="text" value="<?php echo $my_order->phone; ?>"></input></td>
            </tr>
            <tr>
              <td class="title">Email</td>
              <td class="content"><input id="email" type="text" value="<?php echo $my_order->email; ?>"></input></td>
            </tr>
          </table>
        </td>
        <td style="padding-left: 10px;width: 40%;">
          <div>&nbsp;</div>
          <b>&nbsp;</b>
          <table class="form" cellpadding=0 cellspacing=0>
            <tr>
              <td class="title">Order No.</td>
              <td class="content"><?php echo $my_order->order_number; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Date</td>
              <td class="content"><?php echo $my_order->date; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Payment</td>
              <td class="content"><?php echo $my_order->payment_name; ?>&nbsp;</td>
            </tr>
            <tr>
              <td class="title">Courier</td>
              <td class="content"><?php echo $my_order->delivery_name; ?>&nbsp;</td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td colspan=2>
          <div>&nbsp;</div>
          <b>ORDER DETAILS</b>
          <table class="list" cellpadding=0 cellspacing=0>
            <tr>
              <th class="sort">ITEM</th>
              <th class="sort" style="width: 80px;">QTY</th>
              <th class="sort" style="width: 60px;">EACH</th>
              <th class="sort" style="width: 90px;">TOTAL</th>
            </tr>
            <?php foreach ($purchases as $idx => $pur) { ?>
            <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
              <td title="<?php echo $pur->name . (($pur->attr_opt_code != '') ? '(' . $pur->attr_opt_code . ')' : ''); ?>"><?php echo MiscUtils::getShortName($pur->name . (($pur->attr_opt_code != '') ? '(' . $pur->attr_opt_code . ')' : ''), 50); ?></td>
              <td><?php echo $pur->quantity; ?></td>
              <td>$<?php echo $pur->price; ?></td>
              <td id="total<?php echo $idx; ?>">$<?php echo sprintf("%01.2f", $pur->total); ?></td>
            </tr>
            <?php } ?>
            <?php if (count($purchases) == 0) { ?>
            <tr class="none"><td colspan=5>None</td></tr>
            <?php } ?>
          </table>
        </td>
      </tr>
      <tr>
        <td colspan=2>
          <div>&nbsp;</div>
          <table class="form" cellpadding=0 cellspacing=0>
            <tr>
              <td class="title">Comments</td>
            </tr>
            <tr>
              <td class="content"><textarea id="comment" style="resize:none;"><?php echo $my_order->comment; ?>&nbsp;</textarea></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td colspan=2 style="text-align: left">
          <div>&nbsp;</div>
          <div style="margin-left: auto;width: 250px;border: 1px solid #ddd;background: #fff;">
            <table class="form" cellpadding=0 cellspacing=0>
              <tr>
                <td class="title">Total</td>
                <td class="content"><input id="total" type="text" value="<?php echo $my_order->total; ?>"></input></td>
              </tr>
              <tr>
                <td class="title">Delivery Subtotal</td>
                <td class="content">$<?php echo $my_order->delivery_subtotal; ?></td>
              </tr>
            </table>
          </div>
        </td>
      </tr>
    </table>
  </div>
</div>
<div>&nbsp;</div>
<button class="imgbtn add" onclick="save_order();">Update</button>
<div>&nbsp;</div>
<div>&nbsp;</div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../incls/js/checkutils.js"></script>
<script type="text/javascript" src="pop/pop.js"></script>
<script type="text/javascript" src="assets/js/addr.js"></script>
<script type="text/javascript">
  var _item = JSON.parse('<?php echo addslashes(json_encode($my_order)); ?>');

  function save_order() {
    _item.first_name = $('#first_name')[0].value;
    _item.last_name = $('#last_name')[0].value;
    _item.address1 = $('#address1')[0].value;
    _item.address2 = $('#address2')[0].value;
    _item.city = $('#city')[0].value;
    _item.country = $('#country')[0].value;
    _item.zip = $('#zip')[0].value;
    _item.phone = $('#phone')[0].value;
    _item.email = $('#email')[0].value;
    _item.comment = $('#comment')[0].value;
    _item.total = $('#total')[0].value;
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: 'action=save_order&q=' + encodeURIComponent(JSON.stringify(_item)),
      success: function(msg){
        parent.location.reload();
      }
    });
  }
  
  $(function(){
    //NOT GOOD
    parent.document.getElementById('real_last').style.display = 'block';
    
    var div = parent.document.getElementById('<?php echo LAST_NAV; ?>');
    var div_parent = div.parentNode;
    MyUtils.removeChildElements(div_parent);
    var elm;
    elm = parent.document.createElement('a');
    elm.href = '#';
    elm.style.color = '#000';
    elm.style.fontSize = '13px';
    elm.style.fontWeight = 'bold';
    elm.onclick = function() {
      var url = parent.document.URL.replace('&script=index_order', '');
      url = url.replace('&id=<?php echo $my_order->id; ?>', '');
      url = url.replace('#', '');
      parent.location.href = url;
    };
    elm.appendChild(parent.document.createTextNode('Orders'));
    div_parent.appendChild(elm);

    var td;
    td = div_parent.parentNode.insertCell(-1);
    td.style.width = '20px';
    td.style.textAlign = 'center';
    td.style.color = '#aaa';
    td.appendChild(parent.document.createTextNode('>'));
    
    td = div_parent.parentNode.insertCell(-1);
    td.style.height = '25px';
    td.style.color = '#333';
    td.style.fontSize = '13px';
    if (_item && _item.id) {
      td.appendChild(parent.document.createTextNode(MyUtils.getShort(_item.order_number, 25)));
    } else {
      td.appendChild(parent.document.createTextNode('New Order'));
    }
    //NOT GOOD END
  });
</script>
</body>
</html>